package com.rrd.dw.mr.gzip.kafka;

import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.third.faceid.WebankFaceIdData;
import com.ucredit.babel.common.model.third.faceid.WebankFaceIdRecord;
import com.ucredit.babel.common.model.third.faceid.WebankFaceIdResult;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;
import java.util.Random;

/**
 * 腾讯身份证识别
 *
 * @author xj
 */

public class TencentCloudOCRParse extends ParseBase {
    private Text key = new Text();
    private Text val = new Text();

    /**
     * 腾讯身份证识别
     *
     * @param model
     * @return
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        WebankFaceIdRecord obj = (WebankFaceIdRecord) model;
        WebankFaceIdData data = obj.getData();
        WebankFaceIdResult result = data.getResult();

        StringBuilder sb = getBaseInfo(obj, successful);

        //结果返回码
        sb.append(data.getCode() == null ? "" : data.getCode()).append(CRT001);
        //非关键信息错误提示
        sb.append(data.getMsg() == null ? "" : data.getMsg()).append(CRT001);
        //序列号
        sb.append(data.getBizSeqNo() == null ? "" : data.getBizSeqNo()).append(CRT001);
        //人像面识别结果
        sb.append(result.getFrontCode() == null ? "" : result.getFrontCode()).append(CRT001);
        //国徽面识别结果
        sb.append(result.getBackCode() == null ? "" : result.getBackCode()).append(CRT001);
        //订单号
        sb.append(result.getOrderNo() == null ? "" : result.getOrderNo()).append(CRT001);
        //姓名
        sb.append(result.getName() == null ? "" : result.getName()).append(CRT001);
        //性别
        sb.append(result.getSex() == null ? "" : result.getSex()).append(CRT001);
        //民族
        sb.append(result.getNation() == null ? "" : result.getNation()).append(CRT001);
        //出生日期
        sb.append(result.getBirth() == null ? "" : result.getBirth()).append(CRT001);
        //身份证号
        sb.append(result.getIdcard() == null ? "" : result.getIdcard()).append(CRT001);
        //地址
        sb.append(result.getAddress() == null ? "" : result.getAddress()).append(CRT001);
        //签发机关
        sb.append(result.getAuthority() == null ? "" : result.getAuthority()).append(CRT001);
        //有效日期
        sb.append(result.getValidDate() == null ? "" : result.getValidDate()).append(CRT001);
        //人像面照片
        sb.append(result.getFrontPhoto() == null ? "" : result.getFrontPhoto()).append(CRT001);
        //国徽面照片
        sb.append(result.getBackPhoto() == null ? "" : result.getBackPhoto()).append(CRT001);
        //人像面切边照片
        sb.append(result.getFrontCrop() == null ? "" : result.getFrontCrop()).append(CRT001);
        //国徽面切边照片
        sb.append(result.getBackCrop() == null ? "" : result.getBackCrop()).append(CRT001);
        //身份证头像切边照片
        sb.append(result.getHeadPhoto() == null ? "" : result.getHeadPhoto()).append(CRT001);
        //人像面多重告警码
        sb.append(result.getFrontWarnCode() == null ? "" : result.getFrontWarnCode()).append(CRT001);
        //国徽面多重告警码
        sb.append(result.getBackWarnCode() == null ? "" : result.getBackWarnCode()).append(CRT001);
        //OCR操作时间
        sb.append(result.getOperateTime() == null ? "" : result.getOperateTime()).append(CRT001);
        //正面多重告警码
        sb.append(result.getFrontMultiWarning() == null ? "" : result.getFrontMultiWarning()).append(CRT001);
        //反面多重告警码
        sb.append(result.getBackMultiWarning() == null ? "" : result.getBackMultiWarning()).append(CRT001);
        //正面图片清晰度
        sb.append(result.getFrontClarity() == null ? "" : result.getFrontClarity()).append(CRT001);
        //反面图片清晰度
        sb.append(result.getBackClarity() == null ? "" : result.getBackClarity()).append(CRT001);

        setFileNum(20);
        key.set(getKey());
        val.set(sb.toString().replace("\n", " ").replace("\r", " "));
        context.write(key, val);

        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_tencent_ocr_inc";
    }

    @Override
    public String getType() {
        return RecordType.WEBANK_FACEID_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "腾讯身份证识别";
    }
}
